package com.example.sharedpreferences;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
String mr="王鱼",mrsoft="18772368847";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        final EditText edit1=findViewById(R.id.edit1);
        final EditText edit2=findViewById(R.id.edit2);
        Button login=findViewById(R.id.button);
        final SharedPreferences sharedPreferences=getSharedPreferences("mrsoft",MODE_PRIVATE);
        String username= sharedPreferences.getString("username",null);
        String password=sharedPreferences.getString("password",null);
        if (username!=null &&password!=null){
            if (username.equals(mr)&&password.equals(mrsoft)){
                edit1.setText(mr);
                edit2.setText(mrsoft);
            }
        }


          login.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  String name=edit1.getText().toString();
                  String pass=edit2.getText().toString();
                  SharedPreferences.Editor editor=sharedPreferences.edit();
                  if (name.equals(mr)&&pass.equals(mrsoft))
                  {
                      editor.putString("username",name);
                      editor.putString("password",pass);
                      editor.commit();
                      Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                      startActivity(intent);
                      Toast.makeText(MainActivity.this,"已保存账号密码",Toast.LENGTH_SHORT).show();
                  }
                  else
                  {
                      editor.putString("username","");
                      editor.putString("password","");
                      Toast.makeText(MainActivity.this,"账号密码错误",Toast.LENGTH_SHORT).show();
                  }
              }
          });
        }

}
